package RESTfulDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * 模拟post方式请求Servlet，通过http rest方式
 * @author ckz
 *
 */
public class SendPostTest {
	
	/**
	 * 程序入口
	 * @author ckz
	 * @param args
	 */
	public static void main(String[] args) {
		String url = "http://10.22.1.125:8083/portModeTest//pi/restful/inItemInvty/inItemInvty";
		Map<String,Object> map = new HashMap<String, Object>();
		Map<String,Object> headMap = new HashMap<String, Object>();
		Map<String,Object> bodyMap = new HashMap<String, Object>();
		headMap.put("SOURCE", "putian");
		map.put("HEAD", headMap);
		List<EntryBillDet> dets = new ArrayList<EntryBillDet>();
		for(int i=0;i<3;i++){
			EntryBillDet det = new EntryBillDet("CODE0001","G0001",'1',12d,100d,200d);
			dets.add(det);
		}
		EntryBillMas mas = new EntryBillMas("0001","GC0001","XY0001","HT0001","10","CK0001",dets);
		bodyMap.put("DATASET", mas);
		map.put("BODY", bodyMap);
		JSONObject inJson = JSONObject.fromObject(map);
		//System.out.println(inJson);
		NameValuePair params = new NameValuePair("inJson",inJson.toString());
		NameValuePair[] data = {params};
		//调用接口
		String resute = methodPost(url, data);
		
		System.out.println("输入参数(inJson)："+inJson.toString());
		System.out.println("输出参数(outJson)："+resute);
	}

	/**
	 * 使用HttpClient发送Post请求
	 * @author ckz
	 * @param url
	 * @param data
	 * @return
	 */
	public static String methodPost(String url,NameValuePair[] data){
		String response= "";//要返回的response信息  
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod(url);
		// 将表单的值放入postMethod中  
        postMethod.setRequestBody(data);  
        // 执行postMethod  
        int statusCode = 0;  
        try {  
            statusCode = client.executeMethod(postMethod);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
        
        // 301或者302  
        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY  || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {  
            // 从头中取出转向的地址  
            Header locationHeader = postMethod.getResponseHeader("location");  
            String location = null;  
            if (locationHeader != null) {  
                location = locationHeader.getValue();  
                System.out.println("页面跳转:" + location);  
                response= methodPost(location,data);//用跳转后的页面重新请求。  
            } else {  
                System.err.println("本地跳转失败路径为空.");  
            }  
        } else {  
            //状态行
        	//System.out.println(postMethod.getStatusLine());  
            try {  
                response= postMethod.getResponseBodyAsString();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            postMethod.releaseConnection();  
        }  
        return response;  
    } 
	
	
}
